<template>
    <div style="width: 100%">
        <div id="deawer">
            <v-tableTitle :btnConfig="btnConfig" @cancel="cancel" @save="save"></v-tableTitle>
            <div class="detailBox">
                <v-detail ref="detail"></v-detail>
            </div>
            <div class="detailListBox">
                <v-detailList ref="detailList"></v-detailList>
            </div>
        </div>
    </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";
import config from "../Config";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"
import tableTitle from "commons/TableTitle";

export default {
    name: "",
    mixins: [baseMixin, skipMixin, config],
    data() {
        return {
            flag: true,
            detailFlag: false,
            newFlag: false
        };
    },
    created() {
    },
    computed: {
        btnConfig() {
            return [
                {label: this.$t('common.save'), certigier: !this.btnPermNameList.includes('WM06020704'), invalid: "save"},
                {label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM06020705'), invalid: "cancel"},
            ]
        }
    },
    methods: {
        //保存按钮
        save() {
            this.$refs.detail.$children[0].$refs.input.$refs["from"].validate((valid) => {
                let detail = this.$refs.detail.detailData;
                let data = {};
                if (valid) {
                    if (this.$refs.detailList.$children[1].$refs.input) {
                        //下面明细展示出来的走这里
                        this.$refs.detailList.$children[1].$refs.input.$refs["from"].validate((flag) => {
                            if (flag) {
                                let detailList = this.$refs.detailList.detailData;
                                this.detailFlag = this.$refs.detailList.detailsFlag;
                                if (this.newFlag) {
                                    data = {
                                        ...detail,
                                        details: [{
                                            ...detailList,

                                        }]
                                    }
                                } else {
                                    data = {
                                        ...detail,
                                        details: [{
                                            ...detailList,

                                        }]
                                    }
                                }
                                this.http(data);
                            }
                        })
                    } else {


                        //下面明细没有展示出来的走这里


                        data = {
                            ...detail,
                        }
                        this.http(data);
                    }


                }
            })
        },
        async http(data) {
            try {
                let res = await this.$api.post(`/saveRtxTransfer`, data);
                
                    if (this.newFlag) {
                        this.cancel();
                    } else if (this.detailFlag) {
                        this.$refs.detailList.detailsFlag = false;
                        this.$refs.detailList.detailsNew = false;
                        this.$refs.detailList.detailNav = "1";
                    } else {
                        this.cancel();
                    }
                
            } catch (e) {
                this.$message.error(e)
            }
        },
        //弹框取消
        cancel() {
            this.$router.push({name: "interior"});
        },
    },
    mounted() {
        this.newFlag = this.$store.state.newFlag;
        if (Object.keys(this.$store.state.skip).length !== 0) {
            let data = this.$store.state.skip;
            this.detailsFlag = data.detailListFlag;
        }

    },
    watch: {},
    components: {
        "v-detail": detail,
        "v-detailList": detailList,
        "v-tableTitle": tableTitle,
    }
}
</script>

<style scoped>
</style>
